clear all 
set more off


* This code creates Figure 1 and Table 1.
* These are the aggregat labor market transitions
*     between E (employment), U (unemployment), and N (inactivity)


set autotabgraphs on

local msize1 "medsmall"
local msize2 "medlarge"

local color1 "0 76 167"
local color2 "146 0 0"
local color3 "62 150 81"
local colorgb "white"

local color1b "0 76 167"
local color2b "146 0 0"
local color3b "62 150 81"


use "Transitions_EUN_JJ_OO.dta", clear 

* rename transitions to get rid of "1"
drop trans
gen trans = t0 + t1
order trans, after(year)

tempfile data1em
save `data1em'

forvalues ir = 1/2 {
	
	use `data1em', clear
	
	if `ir' == 1 {
		local tablen "Table1A"
	}
	if `ir' == 2 {
		keep if rich == 1
		local tablen "Table1B"
	}
	
	tempfile data12em
	save `data12em'

	local xcounter = 1
	foreach x in EU EN UE NE EEjob_new EEocc_new{

		use `data12em', clear

		if "`x'" == "EU" | "`x'" == "EN" {
			local ymax = 0.15
			local ycut = 0.05
			local gtitle "`x'"
			local gtitle2 "`x'"
		}
		if "`x'" == "UE" | "`x'" == "NE" {
			local ymax = 0.6
			local ycut = 0.15
			local gtitle "`x'"
			local gtitle2 "`x'"
		}
		if "`x'" == "EEjob_new" {
			local gtitle "Job-Job"
			local gtitle2 "JJ"
			local ymax = 0.4
			local ycut = 0.1		

		}
		if "`x'" == "EEocc_new" {
			local gtitle "OccSwitch"
			local gtitle2 "OO"
			local ymax = 0.4
			local ycut = 0.1		
		}
		
		keep if trans == "`x'"	
		
		bys ccode trans: egen xmean = mean(ytrate)
		bys ccode trans: egen gdpmean = mean(AGDP)
		bys ccode trans: gen counter = _n
			
		reg ytrate lcgdp if trans == "`x'", cluster(ccode)
		predict trend_`x'
		sum ytrate if trans == "`x'"

		if `xcounter' == 1 {
			#delimit ;
			outreg2 using "Figures/`tablen'_BasicTransitions_EUN.tex", replace 
			keep(lcgdp) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("`gtitle'")
			nonotes addnote("\textit{Table notes:} Standard errors clustered by country. *p<.05, **p<.01, ***p<.001.");
			#delimit cr
		}
		else {
			#delimit ;
			outreg2 using "Figures/`tablen'_BasicTransitions_EUN.tex", append
			keep(lcgdp) addstat(Sample Average,`r(mean)') nocons label dec(3) adec(3) ctitle("`gtitle'")
			nonotes addnote("\textit{Table notes:} Standard errors clustered by country. *p<.05, **p<.01, ***p<.001.");
			#delimit cr
		}

		if `ir' == 1 {
			#delimit ;
			twoway
			(scatter ytrate AGDP, mcolor("`color1b'%10") msize(`msize1') msymbol(O) mlwidth(none)) 
			(line trend_`x' AGDP, lcolor(gs6) lwidth(medthin) alc(background) lcolor("`color1'%50")) 
			(scatter xmean gdpmean if counter == 1, mcolor("`color1'") msize(`msize2') mlabel(ccode) msymbol(O) mlabcolor("`color1'")) 
			if trans == "`x'" & ytrate <= `ymax', name(`x') legend(off) 
			graphregion(color("`colorgb'")) plotregion(color(none)) bgcolor(none) xtitle("GDP per capita") 
			yscale(r(0 `ymax')) ylabel(0(`ycut')`ymax') xscale(log) xlabel(4000 8000 16000 32000 64000);			
			graph export "Figures/Figure1_`gtitle2'.tif", replace; 
			graph export "Figures/pdf_figures/Figure1_`gtitle2'.pdf", replace; 
			#delimit cr	
		}
		
		local xcounter = `xcounter' + 1

	}


}

